package com.study.springsecurity.config;

import org.apache.commons.lang3.tuple.Triple;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.config.core.GrantedAuthorityDefaults;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;

import javax.sql.DataSource;

/**
 * Security配置Bean
 *
 * @Author YL
 * @Create 2024/3/30 10:48
 * @Version 1.0
 */
@Configuration
public class SecurityBeanConfig {

    /**
     * 配置角色权限的默认前缀
     *
     * @return
     */
    @Bean
    public GrantedAuthorityDefaults createGrantedAuthorityDefaults() {
        return new GrantedAuthorityDefaults("ROLE_");
    }

    /**
     * 配置认证信息token存储位置（内存）
     *
     * @return
     */
    @Bean
    //@Primary
    public InMemoryTokenRepositoryImpl createInMemoryTokenRepositoryImpl() {
        return new InMemoryTokenRepositoryImpl();
    }

    /**
     * 配置认证信息token存储位置（数据库）
     *
     * @return
     */
    @Bean
    @Primary
    public JdbcTokenRepositoryImpl createJdbcTokenRepositoryImpl(DataSource dataSource) {
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        jdbcTokenRepository.setCreateTableOnStartup(true);
        return jdbcTokenRepository;
    }

    /**
     * 回话注册表
     *
     * @return
     */
    @Bean
    public SessionRegistryImpl createSessionRegistry() {
        return new SessionRegistryImpl();
    }
}
